﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MathNet.Numerics.LinearAlgebra;

using cit.CopyDetection.DatabaseAccess;
using cit.CopyDetection.Model;
using cit.CopyDetection.DataObject;

namespace cit.CopyDetection
{
    public partial class frmModelCreation : Form
    {
        private BackgroundWorker matrixCreationWorker;
        private string localWtFunc = "cit.CopyDetection.Model.BinaryWtFunc";
        private string globalWtFunc = "cit.CopyDetection.Model.NoWtFunc";

        public frmModelCreation()
        {
            InitializeComponent();

            matrixCreationWorker = new BackgroundWorker();
            matrixCreationWorker.DoWork += new DoWorkEventHandler(matrixCreationWorker_DoWork);
            matrixCreationWorker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(matrixCreationWorker_RunWorkerCompleted);
        }

        private void matrixCreationWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            CreateModel(); 
        }

        private void matrixCreationWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Error != null)
                MessageBox.Show(e.Error.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                MessageBox.Show("Create model successfully!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Cursor = Cursors.Default;
            EnableControls(true);
        }

        private void btnCreateModel_Click(object sender, EventArgs e)
        {
            EnableControls(false);
            this.Cursor = Cursors.WaitCursor;
            matrixCreationWorker.RunWorkerAsync();
        }

        private void rbtnLocalWtFuncs_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rbtnLocalWtFuncs = (RadioButton)sender;
            this.localWtFunc = rbtnLocalWtFuncs.Tag.ToString();
        }

        private void rbtnGlobalWtFuncs_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton rbtnGlobalWtFuncs = (RadioButton)sender;
            this.globalWtFunc = rbtnGlobalWtFuncs.Tag.ToString();
        }

        private void EnableControls(bool flag)
        {
            foreach (Control ctrl in this.Controls)
            {
                ctrl.Enabled = flag;
            }
        }

        private void CreateModel() 
        {
            ModelCreator modelCreator = new ModelCreator();
            modelCreator.LocalWtFunc = TermWtFuncFactory.GetIntance().CreateLocalWtFunc(localWtFunc);
            modelCreator.GlobalWtFunc = TermWtFuncFactory.GetIntance().CreateGlobalWtFunc(globalWtFunc);

            Dictionary<Document, Position> positions;
            List<ParagraphModel> paraModels = modelCreator.ModelRegisteredDocuments(out positions);
            ModelWriter.WriteModel(ConfigReader.ModelPath, localWtFunc, globalWtFunc, paraModels, positions);
            ModelWriter.WriteVocabulary(ConfigReader.VocabularyPath, TermDataAccess.GetTerms());
        }
    }
}
